<div class="blazorise-codeblock">
<div class="csharp"><pre>
<span class="keyword">public</span> <span class="keyword">class</span> MessageLocalizerHelper&lt;T&gt;
{
    <span class="keyword">private</span> <span class="keyword">readonly</span> Microsoft.Extensions.Localization.IStringLocalizer&lt;T&gt; stringLocalizer;

    <span class="keyword">public</span> MessageLocalizerHelper( Microsoft.Extensions.Localization.IStringLocalizer&lt;T&gt; stringLocalizer )
    {
        <span class="keyword">this</span>.stringLocalizer = stringLocalizer;
    }

    <span class="keyword">public</span> <span class="keyword">string</span> Localize( <span class="keyword">string</span> message, IEnumerable&lt;<span class="keyword">string</span>&gt;? arguments )
    {
        <span class="keyword">try</span>
        {
            <span class="keyword">return</span> arguments?.Count() &gt; <span class="number">0</span>
                ? stringLocalizer[message, LocalizeMessageArguments( arguments )?.ToArray()!]
                : stringLocalizer[message];
        }
        <span class="keyword">catch</span>
        {
            <span class="keyword">return</span> stringLocalizer[message];
        }
    }

    <span class="keyword">private</span> IEnumerable&lt;<span class="keyword">string</span>&gt; LocalizeMessageArguments( IEnumerable&lt;<span class="keyword">string</span>&gt; arguments )
    {
        <span class="keyword">foreach</span> (<span class="keyword">var</span> argument <span class="keyword">in</span> arguments)
        {
            <span class="comment">// first try to localize with &quot;DisplayName:{Name}&quot;</span>
            <span class="keyword">var</span> localization = stringLocalizer[$<span class="string">&quot;DisplayName:{argument}&quot;</span>];

            <span class="keyword">if</span> (localization.ResourceNotFound)
            {
                <span class="comment">// then try to localize with just &quot;{Name}&quot;</span>
                localization = stringLocalizer[argument];

                <span class="keyword">yield</span> <span class="keyword">return</span> localization;
            }
        }
    }
}
</pre></div>
</div>
